<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="java.text.DateFormat"%>
<%@page import="edu.fpt.dal.UsersDAL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
        <link rel="stylesheet" type="text/css" href="reset.css">
        <link rel="stylesheet" type="text/css" href="structure.css">
    </head>
    <body>

        <%
            String saveUsername = "";
            String savePassword = "";
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("saveUser")) {
                        saveUsername = cookie.getValue();
                    }
                    if (cookie.getName().equals("savePassword")) {
                        savePassword = cookie.getValue();
                    }
                }
            }
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            boolean remember = false;
            if (request.getParameter("remember") != null) {
                remember = true;
            }
            if (username == null || password == null) {
        %>
        <form class="box login"  method="post" action="index.jsp">
            <fieldset class="boxBody">
                <label>Username</label>
                <input type="text" name="username" tabindex="1" placeholder="" 
                       required="" value="<%=saveUsername%>">
                <label><a href="#" class="rLink" tabindex="5">
                        Forget your password?</a>Password</label>
                <input type="password" name="password" tabindex="2" required=""  
                       value="<%=savePassword%>">
            </fieldset>
            <footer>
                <label><input type="checkbox" tabindex="3" name="remember" 
                              value="true">Remember me!</label>
                <input type="submit" class="btnLogin" value="Login" tabindex="4">
            </footer>
        </form>



        <%            } else {
                if (UsersDAL.checkLogin(username, password)) {
                    String cookieName = username + "lastAccess";
                    String lastTimeVisit = "Never";
                    if (cookies != null) {
                        for (Cookie cookie : cookies) {
                            if (cookie.getName().equals(cookieName)) {
                                lastTimeVisit = cookie.getValue().toString();
                            }
                        }
                    }
                    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
                    DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
                    java.util.Date date = new java.util.Date();
                    Cookie newcookie = new Cookie(cookieName, timeFormat.format(date) + " " + dateFormat.format(date));
                    newcookie.setMaxAge(31536000);
                    response.addCookie(newcookie);

                    if (UsersDAL.getUserRole(username) == 1) {
                        session.invalidate();
                        session = request.getSession();
                        session.setAttribute("currentAdmin", username);
                        session.setAttribute("lastTimeVisit", lastTimeVisit);
                        if (remember) {
                            Cookie cookie = new Cookie("saveUser", username);
                            cookie.setMaxAge(31536000);
                            response.addCookie(cookie);
                            cookie = new Cookie("savePassword", password);
                            cookie.setMaxAge(31536000);
                            response.addCookie(cookie);
                        }
                        response.sendRedirect("admin/index.jsp");
                    } else {
                        session.invalidate();
                        session = request.getSession();
                        if (remember) {
                            Cookie cookie = new Cookie("saveUser", username);
                            cookie.setMaxAge(31536000);
                            response.addCookie(cookie);
                            cookie = new Cookie("savePassword", password);
                            cookie.setMaxAge(31536000);
                            response.addCookie(cookie);
                        }
                        session.setAttribute("currentUser", username);
                        session.setAttribute("lastTimeVisit", lastTimeVisit);
                        response.sendRedirect("user/index.jsp");
                    }
                } else {
                    response.sendRedirect("index.jsp");
                }
            }

        %>


    </body>
</html>
